package com.beyond.project.dao.impl;

import java.sql.SQLException; 
import java.util.ArrayList;
import java.util.List;

import net.common.dao.GeneralDaoImpl;
import net.common.service.PageBean;
import net.common.util.BeanUtils;

import com.beyond.project.dao.ProjectDAO;
import com.beyond.project.pojo.ProjectData;
import com.beyond.project.vo.ProjectBean;

public class ProjectDAOImpl extends GeneralDaoImpl implements ProjectDAO {

	@SuppressWarnings("unchecked")
	@Override
	public List<ProjectBean> listAll(PageBean page) throws Exception {
		
		List<ProjectBean> list = new ArrayList<ProjectBean>();
		
		StringBuilder hql = new StringBuilder("from ProjectData");
		List<ProjectData> datas = hibernateHandler.query(hql, page);
		
		for(ProjectData unit : datas){
			ProjectBean bean = new ProjectBean();
			BeanUtils.copyProperties(unit, bean);
			list.add(bean);
		}
		return list;
	}

	@Override
	public ProjectBean findOne(Integer id) throws SQLException {
		ProjectData unit = (ProjectData)hibernateHandler.find(ProjectData.class, id);
		
		ProjectBean bean = new ProjectBean();
		BeanUtils.copyProperties(unit,bean);
		
		return bean;
	}

	@Override
	public void insert(ProjectBean bean) throws SQLException {
		ProjectData unit = new ProjectData();
		
		BeanUtils.copyProperties(bean, unit);
		
		hibernateHandler.insert(unit);
	}

	@Override
	public void update(ProjectBean bean) throws SQLException {
		ProjectData unit = (ProjectData)hibernateHandler.find(ProjectData.class, bean.getProjectId());
		BeanUtils.copyProperties(bean, unit);
		hibernateHandler.update(unit);
	}

	@Override
	public void delete(ProjectBean bean) throws SQLException {
		ProjectData unit = (ProjectData)hibernateHandler.find(ProjectData.class, bean.getProjectId());
		hibernateHandler.delete(unit);
	}
}